<?php
/**
 * Created by PhpStorm.
 * User: CCJK
 * Date: 2018/7/11
 * Time: 16:58
 */
header('Access-Control-Allow-Origin:*');
header("Content-Type: text/event-stream\n\n");
header('cache-control:no-cache');

/**
 * 因为你 echo 里面输出的文本内容格式不对。

每一次 echo 的内容，都必须是如下格式

field: value
field 可以是 data, event, id, retry 这四个中的任意一个
value 为数据有效载荷

你这里的例子，应该改为

echo "data: hello world\n\n";
 */
$counter = rand(1, 10);
while (true) {
    // Every second, send a "ping" event.

    echo "event: ping\n";
    $curDate = date(DATE_ISO8601);
    echo 'data: {"time": "' . $curDate . '"}';
    echo "\n\n";

    // Send a simple message at random intervals.

    $counter--;

    if (!$counter) {
        echo 'data: This is a message at time ' . $curDate . "\n\n";
        $counter = rand(1, 10);
    }

    ob_end_flush();
    flush();
    sleep(1);
}
?>